﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Security;

namespace AIC_PN
{
    public partial class FormCanBo : System.Web.UI.Page
    {
        private static int ID;
        private string MyAction = MyConst.Action.Addnew;
        private string MyMaDM = "";


        #region Ham Su kien
        protected void Page_Load(object sender, EventArgs e)
        {
            CheckPermision();
            formUpdate.Visible = false;
            Config();
            if (!IsPostBack)
            {
                BindDataToGrid("");
            }
        }

        private void Config()
        {
            if (MyMaDM == MyConst.DanhMuc.CapBac.Ma)
                lblFormName.InnerText = "Quản trị danh mục " + MyConst.DanhMuc.CapBac.Ten;
            else if (MyMaDM == MyConst.DanhMuc.Quyen.Ma)
                lblFormName.InnerText = "Quản trị danh mục " + MyConst.DanhMuc.Quyen.Ten;
            else if (MyMaDM == MyConst.DanhMuc.NhomUser.Ma)
                lblFormName.InnerText = "Quản trị danh mục " + MyConst.DanhMuc.NhomUser.Ten;
            else if (MyMaDM == MyConst.DanhMuc.TrinhDo.Ma)
                lblFormName.InnerText = "Quản trị danh mục " + MyConst.DanhMuc.TrinhDo.Ten;
            else if (MyMaDM == MyConst.DanhMuc.ChucVu.Ma)
                lblFormName.InnerText = "Quản trị danh mục " + MyConst.DanhMuc.ChucVu.Ten;
            else if (MyMaDM == MyConst.DanhMuc.BaoCaoDinhKem.Ma)
                lblFormName.InnerText = "Quản trị danh mục " + MyConst.DanhMuc.BaoCaoDinhKem.Ten;
            else if (MyMaDM == MyConst.DanhMuc.NoiDungMau1.Ma)
                lblFormName.InnerText = "Quản trị danh mục " + MyConst.DanhMuc.NoiDungMau1.Ten;
            else if (MyMaDM == MyConst.DanhMuc.NoiDungMau8.Ma)
                lblFormName.InnerText = "Quản trị danh mục " + MyConst.DanhMuc.NoiDungMau8.Ten;
            else if (MyMaDM == MyConst.DanhMuc.KyBaoCao.Ma)
                lblFormName.InnerText = "Quản trị danh mục " + MyConst.DanhMuc.KyBaoCao.Ten;
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("Index.aspx");
        }


        protected void grdDanhMuc_RowCommand(object sender, GridViewCommandEventArgs e)
        {

            ID = Convert.ToInt32(e.CommandArgument);
            if (e.CommandName == MyConst.Action.Update)
            {

                formUpdate.Visible = true;
                DataTable DanhMucTable = GetChiTietCanBo(ID);
                txtHoTen.Text = DanhMucTable.Rows[0][1].ToString();
                txtChucVu.Text = DanhMucTable.Rows[0][2].ToString();
                txtDonViID.Text = DanhMucTable.Rows[0][3].ToString();
                lblTenDonVi.InnerHtml = DanhMucTable.Rows[0][8].ToString();
                cboTapThe.SelectedValue = DanhMucTable.Rows[0][4].ToString();
                btnLuu.CommandName = MyConst.Action.Update;
            }
            else if (e.CommandName == MyConst.Action.Delete)
            {
                string sql = "DELETE FROM dbo.tblCanBo WHERE ID = @ID";
                List<SqlParameter> lstParameter = new List<SqlParameter>();
                lstParameter.Add(SqlHelper.CreateParameterObject("@ID", SqlDbType.Int, ParameterDirection.Input, ID));
                SqlHelper.ExecuteNonQuery(CommandType.Text, sql, lstParameter);
                BindDataToGrid("");
                MyCommon.AddMessage(this.Page.Master, "Xóa người dùng thành công", MyConst.MessageType.Success);
            }
        }

        protected void btnThem_Click(object sender, EventArgs e)
        {
            formUpdate.Visible = true;
            txtHoTen.Text = "";
            txtChucVu.Text = "";
            txtDonViID.Text = "";
            btnLuu.CommandName = MyConst.Action.Addnew;
        }

        protected void btnLuu_Click(object sender, EventArgs e)
        {
            int UserID = Convert.ToInt32(Session["userid"]);
            if (btnLuu.CommandName == MyConst.Action.Addnew)
            {
                InsertDanhMuc(
                    txtHoTen.Text,
                    txtChucVu.Text,
                    Convert.ToInt32(txtDonViID.Text),
                    Convert.ToInt32(cboTapThe.SelectedValue));
                BindDataToGrid("");
                MyCommon.AddMessage(this.Page.Master, "Thêm người dùng thành công", MyConst.MessageType.Success);
            }
            else if (btnLuu.CommandName == MyConst.Action.Update)
            {
                UpdateDanhMuc(
                    txtHoTen.Text,
                    txtChucVu.Text,
                    Convert.ToInt32(txtDonViID.Text),
                    Convert.ToInt32(cboTapThe.SelectedValue),
                    ID
                    );
                BindDataToGrid("");
                MyCommon.AddMessage(this.Page.Master, "Cập nhật thông tin người dùng thành công", MyConst.MessageType.Success);
            }
        }

        protected void btnHuy_Click(object sender, EventArgs e)
        {
            formUpdate.Visible = false;
        }
        #endregion Ham su kien

        #region Ham chuc nang

        protected void BindDataToGrid(string HoTen)
        {
            List<SqlParameter> arrParams = new List<SqlParameter>();
            arrParams.Add(SqlHelper.CreateParameterObject("@HoTen", SqlDbType.NVarChar, ParameterDirection.Input, HoTen));
            string sql = "SELECT CB.*,DV.TenDV FROM dbo.tblCanBo CB " +
                         "INNER JOIN dbo.tblDonVi DV ON DV.ID = CB.DonViID " +
                         "WHERE TenCanBo LIKE '%'+@HoTen+'%' OR @HoTen = ''";
            grdDanhMuc.DataSource = SqlHelper.ExecuteDataTable(CommandType.Text, sql, arrParams);
            grdDanhMuc.DataBind();
        }

        protected DataTable GetChiTietCanBo(int ID)
        {
            List<SqlParameter> arrParams = new List<SqlParameter>();
            arrParams.Add(SqlHelper.CreateParameterObject("@ID", SqlDbType.Int, ParameterDirection.Input, ID));
            return SqlHelper.ExecuteDataTable(CommandType.Text, "SELECT CB.*,DV.TenDV FROM dbo.tblCanBo CB INNER JOIN dbo.tblDonVi DV ON DV.ID = CB.DonViID WHERE CB.ID = @ID", arrParams);
        }

        protected void InsertDanhMuc(string HoTen, string ChucVu, int DonViID, int LaToChuc)
        {
            List<SqlParameter> arrParams = new List<SqlParameter>();
            arrParams.Add(SqlHelper.CreateParameterObject("@HoTen", SqlDbType.NVarChar, ParameterDirection.Input, HoTen));
            arrParams.Add(SqlHelper.CreateParameterObject("@ChucVu", SqlDbType.NVarChar, ParameterDirection.Input, ChucVu));
            arrParams.Add(SqlHelper.CreateParameterObject("@DonViID", SqlDbType.Int, ParameterDirection.Input, DonViID));
            arrParams.Add(SqlHelper.CreateParameterObject("@LaToChuc", SqlDbType.Int, ParameterDirection.Input, LaToChuc));
            arrParams.Add(SqlHelper.CreateParameterObject("@UserID", SqlDbType.Int, ParameterDirection.Input, Session["userid"]));
            string sql = "INSERT INTO dbo.tblCanBo(TenCanBo ,ChucVu ,DonViID ,LaToChuc ,CreateDate ,UpdateDate ,UserID)" +
                         "VALUES(@HoTen ,@ChucVu ,@DonViID ,@LaToChuc ,GETDATE() ,GETDATE() ,@UserID)";
            SqlHelper.ExecuteNonQuery(CommandType.Text, sql, arrParams);
        }

        protected void UpdateDanhMuc(string HoTen, string ChucVu, int DonViID, int LaToChuc, int ID)
        {
            List<SqlParameter> arrParams = new List<SqlParameter>();
            arrParams.Add(SqlHelper.CreateParameterObject("@HoTen", SqlDbType.NVarChar, ParameterDirection.Input, HoTen));
            arrParams.Add(SqlHelper.CreateParameterObject("@ChucVu", SqlDbType.NVarChar, ParameterDirection.Input, ChucVu));
            arrParams.Add(SqlHelper.CreateParameterObject("@DonViID", SqlDbType.Int, ParameterDirection.Input, DonViID));
            arrParams.Add(SqlHelper.CreateParameterObject("@LaToChuc", SqlDbType.Int, ParameterDirection.Input, LaToChuc));
            arrParams.Add(SqlHelper.CreateParameterObject("@UserID", SqlDbType.Int, ParameterDirection.Input, Session["userid"]));
            arrParams.Add(SqlHelper.CreateParameterObject("@ID", SqlDbType.Int, ParameterDirection.Input, ID));
            string sql = "UPDATE dbo.tblCanBo SET TenCanBo = @HoTen, ChucVu = @ChucVu, DonViID = @DonViID,LaToChuc = @LaToChuc, UpdateDate = GETDATE(), UserID = @UserID WHERE ID = @ID";
            SqlHelper.ExecuteNonQuery(CommandType.Text, sql, arrParams);
        }

        public void CheckPermision()
        {
            Dictionary<string, int> Role = MyCommon.CheckPermission("DanhMucCanBo");
            if (Role == null || Role["Xem"] == 0)
            {
                Response.Redirect("Index.aspx?p=0");
            }
        }
        #endregion  Ham chuc nang

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindDataToGrid(txtSearch.Text.Trim());
        }
    }
}
